var c_num = '';
apiready = function(){
api.addEventListener({
    name:'viewdisappear'
}, function(ret, err){        
   api.closeWin();
});

api.ajax({
        url:'http://wap.jqmao.cn/app/mobile/getpayConfig'
    },function(ret,err){
   
    	//coding...

    	switch(ret.value){
    		case '1':
    			var wx_ = $api.byId('wxpay');
	            $api.css(wx_,'display:none');
	            $api.removeCls(wx,'select');
	   			$api.addCls(jingpay,'select');
	   			paytype = 'jingapp';
    		break;
    		case '2':
    			var jin_ = $api.byId('jingpay');
	            $api.css(jin_,'display:none');
	            $api.removeCls(jin_,'select');
	   			$api.addCls(wx,'select');
	   			paytype = 'wxpay';
    		break;
    	}
    });
var systemType = api.systemType;
if(systemType=='ios'){
		 var wx = api.require('wx');
	     wx.isInstalled(function(ret, err) {
		 if(ret.installed) {
		      //alert("当前设备已安装微信客户端");
		     iswx = true;
		  } else {
		       	var wx_ = $api.byId('wxpay');
	            $api.css(wx_,'display:none');
	            $api.removeCls(wx,'select');
	   			$api.addCls(jingpay,'select');
	   			paytype = 'jingapp';
		  }
		});			
	}else{
		iswx = true;
	}



if(api.pageParam.id){
c_num = api.pageParam.id;
}
	
	headerstatus()
    changeNum() 
    if(c_num!=''){
    	$api.html(num,c_num+'.00');
    	var cc = $api.byId(''+c_num);
    	var dd = $api.byId('10');
    	$api.addCls(cc, 'z-sel');
    	$api.removeCls(dd, 'z-sel');
    } 

}


var user = $api.getStorage('user');

var money = 10;
var num = $api.byId('num');

var b = null;
 var h = /^[1-9]{1}\d*$/;
 var j = "";
 var a = null;
 var wx = $api.byId('radio-1-1');
   	var jingpay = $api.byId('radio-1-2');
	  var button = $api.byId('button');
var paytype = 'wxpay';

  function l(m) {
            m = Math.round(m * 1000) / 1000;
            m = Math.round(m * 100) / 100;
            if (/^\d+$/.test(m)) {
                return m + ".00"
            }
            if (/^\d+\.\d$/.test(m)) {
                return m + "0"
            }
            alert(m)
            return m
        }
 var act = function() {

            var m = a.val();
           

            if (m != "") {
                if (j != m) {
                    if (!h.test(m)) {
                        a.val(j).focus()
                    } else {
                        j = m;
                        $api.html(num,l(m));
                     
                    }
                }
            } else {
       
                j = "";
                a.focus();
               $api.html($api.byId('num'),"0.00");
            }
        }

 
var changeNum = function(){

	var num = $api.byId('num')
	/* var ul = $api.byId('g-Rechange');*/
	 var li = $api.domAll('#g-Rechange li');
	 $.each(li, function(key, value){
	 		//alert(value)
	 		var n = $(this);
	 		
	 		if (key < 8) {
	 		
	 			
		 		$api.addEvt(value, 'click', function(){
		 		c_num = ''
		 		 	money = $api.attr(value,'money');
		 		 	$api.html(num,l(money));
		 		 	 n.children('a').addClass("z-sel");
		 		 	 n.siblings().children().removeClass("z-sel").removeClass("z-initsel");
				});	 
	 		}else{
	 			 	a = n.find("input");
                	a.focus(function() {
               
                    g = true;
            
                    if (a.val() == "输入金额") {
                        a.val("")
                    }
                    a.parent().addClass("z-initsel").parent().siblings().children().removeClass("z-sel");
                      
                    if (b == null) {
                   
                        b=setInterval(function(){
                        act();
                        }, 200)
                    }
                    
                }).blur(function() {
                    clearInterval(b);
                    b = null
                })
	 		}

	 })

	  $api.addEvt(button,'click',function(e){
	  		
	  		if(e.target.getAttribute('data-pro')=='wx'){

	  		paytype = 'wxpay';
   			
   			/*$api.attr(jingpay,'checked','false')
   			$api.attr(balance,'checked','false');*/
   			
   			$api.addCls(wx,'select');
			$api.removeCls(jingpay,'select');
   			
   			
   			}else if(e.target.getAttribute('data-pro')=='jingpay'){
   			
   			paytype = 'jingapp';
   			/*$api.attr(wx,'checked','false');
   			$api.attr(balance,'checked','false');*/
   			$api.removeCls(wx,'select');
   			$api.addCls(jingpay,'select');

   		}
	  })

		/*setInterval(reload, 200)*/
		//$api.html(num,'50.00');

}


var btnSubmit = $api.byId("btnSubmit");

	  $api.addEvt(btnSubmit,'click',function(e){
	  	money = parseInt($api.html(num));
	  	if(c_num!=''){
	  		money = c_num;
	  	}
	  	
	  	//alert(money)
	  	
	  	var uid = user.uid;
			//api.closeWin();
			var systemType = api.systemType;
			
			if(paytype=='wxpay'){
			
				if(systemType=='ios'){		
			    		//获取微信支付			    		
			    		api.ajax({
                            url:'http://wap.jqmao.cn/getpay'
                        },function(ret,err){
                        	//coding...
                        	if(ret.show=='1'){
                        		openUrl("http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/"+ret.pay_class+"/"+uid ,'支付','wx')
                        	}else{
                        		openUrl("http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/"+ret.pay_class+"/"+uid ,'支付','wx2')
                        	}
                        });
			    		//location.href="http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/jupay/"+uid;  							   
				}else{
					//location.href="http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/jupay/"+uid;
					//openUrl("http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/"+paytype+"/"+uid ,'支付','wx')
					//获取微信支付
				
				    		api.ajax({
	                            url:'http://wap.jqmao.cn/getpay'
                            },function(ret,err){
                            	//coding...
                            	if(ret.show=='1'){
                            		openUrl("http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/"+ret.pay_class+"/"+uid ,'支付','wx')
                            	}else{
                            		openUrl("http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/"+ret.pay_class+"/"+uid ,'支付','wx2')
                            	}
                            });
				}
			
				//openUrl("http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/"+paytype+"/"+uid ,'支付','wx')
			}else{
				if(systemType=='ios'){				
				    	//coding...
				    	//alert(ret.value)	
				    	//alert(ret.value)
				    	api.ajax({
	                        url:'http://wap.jqmao.cn/mobile/cart/getOrderCode'
                        },function(ret,err){
                     		var ordercode = ret.dingdancode;
                        	//coding...
                        	if(ordercode){   		
                        		var u ='http://wap.jqmao.cn/mobile/cart/appaddmoney/'+money+'/'+paytype+'/'+uid+'/'+ordercode;
								api.openApp({
					                iosUrl: u,
					               
				                },function(ret,err){
				                	//coding...
				                }); 
				                var dialogBox = api.require('dialogBox');
						dialogBox.alert({
						    texts: {
						       
						        content: '支付中',
						        leftBtnTitle: '支付遇到问题',
						        rightBtnTitle: '支付成功'
						    },
						    styles: {
						        bg: '#fff',
						        w: 300,
						       
						        content: {
						            color: '#000',
						            size: 18
						        },
						        left:{      
						            marginB: 7,
						            marginL: 18,
						            w: 130,
						            h: 35,
						            corner: 2,        
						            bg: '#fff',     
						            color: '#007FFF',
						            size: 14
						        },
						        right: {    
						            marginB: 7,   
						            marginL: 18,
						            w: 130,  
						            h: 35,   
						            corner: 2,  
						            bg: '#fff',
						            color: '#007FFF',  
						            size: 14              
						        }
						    }
						}, function(ret) {
						    if(ret.eventType == 'left') {  
						        dialogBox.close({
						            dialogName: 'alert'
						        });
						    }else{
						    //alert(ordercode)
						    	//alert(ordercode);
						    	api.ajax({
	                                url:'http://wap.jqmao.cn/appGetOrderData/?code='+ordercode
                                },function(ret,err){
                                	//coding...
                                	
                                	//alert(JSON.stringify(ret))
                                	if(ret.code==0){
                                		alert('获取支付结果失败，请重试!')
                                	}else{
                                	
                                		 if (ret.redcode) {
                                		 api.openWin({
								                name: 'payResult',
								                url: 'widget://html/payResult.html',
								                pageParam: {
								                    r_code: '' + ret.redcode,
								                    recharge: '' + true
								                },
								                animation: {
								                    type: "none",                //动画类型（详见动画类型常量）
								                }
								            });
											
								           
								        } else {
								         
								       
								            api.openWin({
								                name: 'payResult',
								                url: 'widget://html/payResult.html',
								                pageParam: {
								                    recharge: '' + true
								                },
								                animation: {
								                    type: "none",                //动画类型（详见动画类型常量）
								                }
								
								            });
								        }
                                	}
                                	
                                });
						    }
						});
				                
		                    }
                        });
			  				
					
				}else{				
					openUrl("http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/"+paytype+"/"+uid,'支付','jingpay');
				}
				//openUrl("http://wap.jqmao.cn/mobile/cart/appaddmoney/"+money+"/"+paytype+"/"+uid ,'支付','jingpay')
			}
            			
			
			
	  })
	  
	 
